<?php

namespace app\admin\controller\iot\equipment_management;

use app\admin\service\iot\equipment_management\EquipmentManagementIot;
use app\BaseController;
use app\common\Message;
use app\model\iot_model\IotRoom;
use app\model\PlatformModel;
use app\model\UnitModel;
use think\App;
use think\response\Json;

class EquipmentManagement extends BaseController
{
    use Message;

    private EquipmentManagementIot $equipmentManagementIotModel;

    private PlatformModel $platformModel;
    private mixed $data;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $data = $this->request->param();
        $data['uid'] = $this->userData['id'];
        $data['role_mode'] = $this->userData['role_mode'];
        //单位id
        $data['unit_id'] = $this->userData['unit_id'];
        $data['company_id'] = $this->userData['company_id'];
        $data['areas_id'] = $this->userData['areas_id'];
        $this->equipmentManagementIotModel = new EquipmentManagementIot();
        $this->data = $data;
    }

    /**
     * 获取层级树
     * @return Json|bool|string
     */
    public function getLevelTree(): bool|string|Json
    {
        try {
            $where = [];
            $where[] = ['id','=',$this->userData['unit_id']];
            $unit = UnitModel::query()->where($where)->field('id,name')->find();
            $unit['num'] = 0;
            if(!empty($unit)){
                //获取每个单位下面的设备总数
                $unit['num'] = IotRoom::query()->where('unit_id',$unit['id'])->field('id')->count();
            }
            return success($unit);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 新增功能室
     * @return bool|string|Json
     */
    public function addRoom(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->addRoom($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 新增功能室列表
     * @return bool|string|Json
     */
    public function getRoomList(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getRoomList($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除功能室
     * @return bool|string|Json
     */
    public function delRoom(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->delRoom($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取设备列表
     * @return bool|string|Json
     */
    public function getRoomDevice(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getRoomDevice($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取设备分类
     * @return bool|string|Json
     */
    public function getDeviceCategory(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getDeviceCategory($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 新增设备
     * @return bool|string|Json
     */
    public function addRoomDevice(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->addRoomDevice($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取设备列表
     * @return bool|string|Json
     */
    public function getDeviceList(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getDeviceList($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取设备列表所有ID
     * @return bool|string|Json
     */
    public function getDeviceAllData(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getDeviceAllData($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 编辑设备
     * @return bool|string|Json
     */
    public function editDevice(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->editDevice($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除设备
     * @return bool|string|Json
     */
    public function delDevice(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->delDevice($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取设备基本情况
     * @return bool|string|Json
     */
    public function getDeviceInfo(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getDeviceInfo($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取设备的用户列表
     * @return bool|string|Json
     */
    public function getDeviceUsers(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getDeviceUsers($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取设备用户的使用记录
     * @return bool|string|Json
     */
    public function getUsageRecord(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getUsageRecord($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取设备的班级
     * @return bool|string|Json
     */
    public function getClassList(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getClassList($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     *获取用户使用记录
     * @return bool|string|Json
     */
    public function getUserDetails(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getUserDetails($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 审核咨询
     * @return bool|string|Json
     */
    public function audit(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->audit($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取消息详情
     * @return Json|bool|string
     */
    public function getMessageDetails(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->getMessageDetails($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 回复消息
     * @return Json|bool|string
     */
    public function replyMessage(): Json|bool|string
    {
        try {
            if(!$this->equipmentManagementIotModel->replyMessage($this->data)) throw new \Exception($this->equipmentManagementIotModel->getMessage());
            return success($this->equipmentManagementIotModel->getResult(),$this->equipmentManagementIotModel->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

}